﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MageWars.GameElements;
using Microsoft.Xna.Framework;
using MageWars.Support.StateMachines.Behaviors;

namespace MageWars.Support.StateMachines.States
{
    public class Guard : State
    {
        private Seek CurrentBehavior { get; set; }

        private const int Radio = 100;

        private static Random Rand = new Random();

        public Guard(Mage Self, Point Center)
            : base(Self)
        {
            this.Center = Center;
            CurrentBehavior = new Seek(Self, Center);

        }

        public Guard(Mage Self, Vector2 Center)
            : this(Self, new Point((int)Center.X, (int)Center.Y))
        {
        }

        public Guard(Mage Self, Vector3 Center)
            : this(Self, new Point((int)Center.X, (int)Center.Y))
        {
        }

        public override Node[] GetCamino()
        {
            return CurrentBehavior.GetCamino();
        }

        private static Random rand = new Random();

        public override MWAction GetNextAction()
        {
            Mage target = null;
            float targetDistance = Attack.Range;
            float distance;
            foreach (var m in Mage.Mages)
            {
                if (m.Group != Self.Group)
                {
                    distance = MageMath.GetDistance(m.Position, Self.Position);
                    if (distance <= targetDistance)
                    {
                        target = m;
                        targetDistance = distance;
                    }
                }
            }

            if (target != null)
            {
                //if (Self.HP < 30)
                //{
                //    return new MWAction() { NewState = new Flee(Self) };
                //}
                //else
                //{
                //    return new MWAction() { NewState = new Attack(Self) };
                //}
                return new MWAction() { NewState = new Attack(Self) };
            }
            else
            {
                var r = CurrentBehavior.GetNextAction();
                if (r.Finished)
                {
                    int cX = Center.X + Rand.Next(Radio) * (Rand.Next(2) == 1 ? 1 : -1);
                    int cY = Center.Y + Rand.Next(Radio) * (Rand.Next(2) == 1 ? 1 : -1);

                    int i = 0;

                    while (!MageMath.IsTargetTransitable(new Point(cX, cY)) && i < 10)
                    {
                        cX = Center.X + Rand.Next(Radio) * (Rand.Next(2) == 1 ? 1 : -1);
                        cY = Center.Y + Rand.Next(Radio) * (Rand.Next(2) == 1 ? 1 : -1);
                        i++;
                    }
                    if (i < 10)
                    {
                        CurrentBehavior.target_position = new Vector2(cX, cY);
                    }
                    else
                    {
                        CurrentBehavior.target_position = new Vector2(Self.Position.X, Self.Position.Y);
                    }
                    return r;
                }
                else
                {
                    double randr = rand.NextDouble();
                    if (randr < 0.01)
                    {
                        randr = rand.NextDouble();
                        if (randr < 0.5)
                        {
                            foreach (var m in Mage.Mages)
                            {
                                if (m.Group == Self.Group)
                                {
                                    return new MWAction() { NewState = new SeekSM(Self, m.Position) };
                                }
                            }
                        }
                        else
                        {
                            foreach (var m in Mage.Mages)
                            {
                                if (m.Group != Self.Group & !m.IsDead)
                                {
                                    return new MWAction() { NewState = new SeekSM(Self, m) };
                                }
                            }

                        }
                        return r;
                    }
                    else
                    {
                        return r;
                    }

                }
            }
        }

        public Point Center { get; set; }
    }
}